home *** CD-ROM | disk | FTP | other *** search
/ Team Palmtops 7 / Palmtops_numero07.iso / WinCE / SDKWindowsCE / HandHeldPCPro30 / sdk.exe / Jupiter SDK / data1.cab / MFC / src / winmenu.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-02-19  |  3.7 KB  |  157 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10.  
  11. #include "stdafx.h"
  12.  
  13. #ifdef AFX_CORE1_SEG
  14. #pragma code_seg(AFX_CORE1_SEG)
  15. #endif
  16.  
  17. #ifdef _DEBUG
  18. #undef THIS_FILE
  19. static char THIS_FILE[] = __FILE__;
  20. #endif
  21.  
  22. #define new DEBUG_NEW
  23.  
  24. /////////////////////////////////////////////////////////////////////////////
  25. // Map from HMENU to CMenu *
  26.  
  27. #if !defined(_WIN32_WCE)
  28. #include "fixalloc.h"
  29.  
  30. class CTempMenu : public CMenu
  31. {
  32.     DECLARE_DYNCREATE(CTempMenu)
  33.     DECLARE_FIXED_ALLOC(CTempMenu);
  34. };
  35. #endif // _WIN32_WCE
  36.  
  37. CHandleMap* PASCAL afxMapHMENU(BOOL bCreate)
  38. {
  39.     AFX_MODULE_THREAD_STATE* pState = AfxGetModuleThreadState();
  40.     if (pState->m_pmapHMENU == NULL && bCreate)
  41.     {
  42.         BOOL bEnable = AfxEnableMemoryTracking(FALSE);
  43. #ifndef _AFX_PORTABLE
  44.         _PNH pnhOldHandler = AfxSetNewHandler(&AfxCriticalNewHandler);
  45. #endif
  46.         pState->m_pmapHMENU = new CHandleMap(WCE_IF(RUNTIME_CLASS(CMenu),RUNTIME_CLASS(CTempMenu)),
  47.             offsetof(CMenu, m_hMenu)),
  48.  
  49. #ifndef _AFX_PORTABLE
  50.         AfxSetNewHandler(pnhOldHandler);
  51. #endif
  52.         AfxEnableMemoryTracking(bEnable);
  53.     }
  54.     return pState->m_pmapHMENU;
  55. }
  56.  
  57. CMenu* PASCAL CMenu::FromHandle(HMENU hMenu)
  58. {
  59.     CHandleMap* pMap = afxMapHMENU(TRUE); // create map if not exist
  60.     ASSERT(pMap != NULL);
  61.     CMenu* pMenu = (CMenu*)pMap->FromHandle(hMenu);
  62.     ASSERT(pMenu == NULL || pMenu->m_hMenu == hMenu);
  63.     return pMenu;
  64. }
  65.  
  66. CMenu* PASCAL CMenu::FromHandlePermanent(HMENU hMenu)
  67. {
  68.     CHandleMap* pMap = afxMapHMENU();
  69.     CMenu* pMenu = NULL;
  70.     if (pMap != NULL)
  71.     {
  72.         // only look in the permanent map - does no allocations
  73.         pMenu = (CMenu*)pMap->LookupPermanent(hMenu);
  74.         ASSERT(pMenu == NULL || pMenu->m_hMenu == hMenu);
  75.     }
  76.     return pMenu;
  77. }
  78.  
  79. /////////////////////////////////////////////////////////////////////////////
  80. // CMenu
  81.  
  82. #ifdef _DEBUG
  83. void CMenu::AssertValid() const
  84. {
  85.     CObject::AssertValid();
  86.     ASSERT(m_hMenu == NULL || ::WCE_FCTN(IsMenu)(m_hMenu));
  87. }
  88.  
  89. void CMenu::Dump(CDumpContext& dc) const
  90. {
  91.     CObject::Dump(dc);
  92.  
  93.     dc << "m_hMenu = " << (UINT)m_hMenu;
  94.     dc << "\n";
  95. }
  96. #endif
  97.  
  98. BOOL CMenu::Attach(HMENU hMenu)
  99. {
  100.     ASSERT(m_hMenu == NULL);        // only attach once, detach on destroy
  101.     if (hMenu == NULL)
  102.         return FALSE;
  103.     CHandleMap* pMap = afxMapHMENU(TRUE); // create map if not exist
  104.     ASSERT(pMap != NULL);
  105.     pMap->SetPermanent(m_hMenu = hMenu, this);
  106.     return TRUE;
  107. }
  108.  
  109. HMENU CMenu::Detach()
  110. {
  111.     HMENU hMenu;
  112.     if ((hMenu = m_hMenu) != NULL)
  113.     {
  114.         CHandleMap* pMap = afxMapHMENU(); // don't create if not exist
  115.         if (pMap != NULL)
  116.             pMap->RemoveHandle(m_hMenu);
  117.     }
  118.     m_hMenu = NULL;
  119.     return hMenu;
  120. }
  121.  
  122. BOOL CMenu::DestroyMenu()
  123. {
  124.     if (m_hMenu == NULL)
  125.         return FALSE;
  126.     return ::DestroyMenu(Detach());
  127. }
  128.  
  129. /////////////////////////////////////////////////////////////////////////////
  130. // Self-drawing menu items
  131.  
  132. void CMenu::DrawItem(LPDRAWITEMSTRUCT /* lpDrawItemStruct */)
  133. {
  134.     // default drawing does nothing
  135. }
  136.  
  137. void CMenu::MeasureItem(LPMEASUREITEMSTRUCT /* lpMeasureItemStruct */)
  138. {
  139.     // default drawing does nothing
  140. }
  141.  
  142. #ifdef AFX_INIT_SEG
  143. #pragma code_seg(AFX_INIT_SEG)
  144. #endif
  145.  
  146. IMPLEMENT_DYNCREATE(CMenu, CObject)
  147.  
  148. #if !defined(_WIN32_WCE)
  149. IMPLEMENT_DYNCREATE(CTempMenu, CMenu);
  150.  
  151. #pragma warning(disable: 4074)
  152. #pragma init_seg(compiler)
  153. IMPLEMENT_FIXED_ALLOC(CTempMenu, 64);
  154. #endif // _WIN32_WCE
  155.  
  156. /////////////////////////////////////////////////////////////////////////////
  157.